<?php
namespace app\admin\controller;
use app\admin\controller\Permissions;
use think\Config;
use \think\Db;
use \think\Cookie;
use \think\Session;
use \think\Cache;
use app\admin\model\Population as populationModel;



class Population extends Permissions
{
	//年龄
	function ageVerification($code){
	    $age_time = strtotime(substr($code, 6, 8));
	    if($age_time === false){
	        return false;
	    }
	    list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age_time)); 
	 
	    $now_time = strtotime("now");
	 
	    list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now_time));
	    $age = $y2 - $y1;
	    if((int)($m2.$d2) < (int)($m1.$d1)){
	        $age -= 1;
	    }
	    return $age; 
	}

    public function daoru()
    {
        $import = input('import', 0);
        if ($import) {  //是导入
            $post = $this->request->post();
            $file_url = ROOT_PATH . 'public' . geturl($post['file_id']);

            $objExcel = new \PHPExcel();
            //读取文件数据开始
            $extension = strtolower(pathinfo($file_url, PATHINFO_EXTENSION));
            if ($extension == 'xlsx') {
                $objReader = new \PHPExcel_Reader_Excel2007();
                $objExcel = $objReader->load($file_url);
            } else if ($extension == 'xls') {
                $objReader = new \PHPExcel_Reader_Excel5();
                $objExcel = $objReader->load($file_url);
            }

            $excel_array = $objExcel->getsheet(0)->toArray();   //转换为数组格式
            array_shift($excel_array);  //删除第一个数组(标题);
            $data = [];
            foreach ($excel_array as $k => $v) {
                if(!$v[3] && !$v[5] && !$v[7]) continue;
                $data[$k]["chainum"] = $v[1];
                $data[$k]["pingnum"] = $v[2];
                $data[$k]["name"] = $v[3];
                $data[$k]["family"] = $v[4];
                $data[$k]["relation"] = $v[5];
                $data[$k]["sex"] = $v[6];
                $data[$k]["age"] = $v[7];
                $data[$k]["idcar"] = $v[8];
                $data[$k]["iscun"] = $v[9];
                $data[$k]["louhao"] = $v[10];
                $data[$k]["status"] = $v[11];
                $data[$k]["isdel"] = 1;
            }
            $success = 0;
            $error = 0;
            $sum = count($data);
            foreach ($data as $k => $v) {
                $model = new populationModel();
                $result = $model->insert($v);
                if ($result !== false) {
                    $success++;
                } else {
                    $error++;
                }
            }
            return $this->success("总{$sum}条，成功{$success}条，失败{$error}条。", 'index');
        }
        return $this->fetch();
    }



    public function index()
    {
    	$list = db('population')->where(['isdel'=>1])->select();
    	foreach ($list as $key => &$v) {
            switch ($v['status']) {
                case '1':
                    $v['status'] = '已完成';
                    break;
                case '2':
                    $v['status'] = '已签';
                    break;
                case '3':
                    $v['status'] = '已联系';
                    break;
                case '4':
                    $v['status'] = '未联系';
                    break;
                
                default:
                    # code...
                    break;
            }
        $v['child'] = db('popinfo')->where(['isdel'=>1,'pid'=>$v['id']])->select();
        $louhao = explode(',',$v['louhao']);

            foreach ($louhao as &$vv) {
                $vv = db('louhao')->where(['id'=>$vv])->value('louhao');
            }
            // dump($louhao);die;
            $v['louhao'] = implode(',',$louhao);


    	}

        $admin_cate_id = Session::get('admin_cate_id');
        $this->assign('admin_cate_id',$admin_cate_id);
        $operation = Config::get('operation');
        $this->assign('operation',$operation);


    	$this->assign('list',$list);
        return $this->fetch();
    }

    //新增
    public function edit()
    {
        $input = input('get.');
        $data = db('population')->where(['id' => $input['id']])->find();
        
        $louhao = explode(',',$data['louhao']);
        foreach ($louhao as &$vv) {
            $vv = db('louhao')->where(['id'=>$vv])->value('louhao');
        }
      
        $data['louhao'] = implode(',',$louhao);
        $this->assign('data',$data);
        
        return $this->fetch();
      
      
    }
    public function deleteData()
    {
        if (request()->isAjax()) {
            $id = input('id');
            $isok = db('population')->where('id', $id)->delete();
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
            } else {
                $arr['code'] = 2;
            }
            return $arr;
        }
    }
    public function editData()
    {
        if (request()->isAjax()) {
            $input = input('post.');
           
                $post = input('post.');
                $whe['id'] = trim($post['id']);
                $whe['chainum'] = trim($post['chainum']);
                $whe['pingnum'] = trim($post['pingnum']);
                $whe['name'] = trim($post['name']);
                $whe['family'] = trim($post['family']);
                $whe['relation'] = trim($post['relation']);
                $whe['sex'] = trim($post['sex']);
                $whe['age'] = trim($post['age']);
                $whe['idcar'] = trim($post['idcar']);
                $whe['iscun'] = trim($post['iscun']);
                $whe['status'] = trim($post['status']);
                // 加一个楼号


                // $louhao = explode(',',$post['louhao']);
                // foreach ($louhao as &$vv) {
                //     $vv = db('louhao')->where(['id'=>$vv])->value('louhao');
                // }
                // $whe['louhao'] = implode(',',$louhao);


                //$whe['louhaoAAAA'] = trim($post['louhaoAAAA']);
                $whe['isdel'] = 1;
                $isok = db('population')->where(['id' =>$whe['id']])->update($whe);
               
                $arr = [];
                if ($isok) {
                    $arr['code'] = 1;
                } else {
                    $arr['code'] = 2;
                }
                return $arr;
           
        }
    }
    public function add()
    {
        if (request()->isAjax()) {
            $input = input('post.');
            $res = db('population')->where(['idcar' => $input['idcar']])->find();
            if (empty($res)) {
                $post = input('post.');
                $whe['chainum'] = trim($post['chainum']);
                $whe['pingnum'] = trim($post['pingnum']);
                $whe['name'] = trim($post['name']);
                $whe['family'] = trim($post['family']);
                $whe['relation'] = trim($post['relation']);
                $whe['sex'] = trim($post['sex']);
                $whe['age'] = trim($post['age']);
                $whe['idcar'] = trim($post['idcar']);
                $whe['iscun'] = trim($post['iscun']);
                // 加一个楼号
                //$whe['louhaoAAAA'] = trim($post['louhaoAAAA']);
                $whe['isdel'] = 1;
                $isok = db('population')->insert($whe);
                $arr = [];
                if ($isok) {
                    $arr['code'] = 1;
                } else {
                    $arr['code'] = 2;
                }
                return $arr;
            } else {
                $this->error('身份证号重复');
            };
        }
    	return $this->fetch();
    }
    //户主档案
    public function info()
    {
    	$pid = input('id');
    	$zhu = db('population')->where(['id'=>$pid])->find();
    	$zhu['chairen'] = db('popinfo')->where(['isdel'=>1,'pid'=>$pid])->count('id');
    	$zhu['chaicun'] = db('popinfo')->where(['isdel'=>1,'pid'=>$pid,'iscun'=>'是'])->count('id');
    	$zhu['chaifei'] = db('popinfo')->where(['isdel'=>1,'pid'=>$pid,'iscun'=>'否'])->count('id');
    	$list = db('popinfo')->where(['isdel'=>1,'pid'=>$pid])->select();
    	foreach ($list as $k => &$v) {
    		$v['age_xian'] = $this->ageVerification($v['idcar']);
    		if ($v['age_xian'] >= 60) {
    			$v['name'] = '<span style="color:red">'.$v['name'].'</span>';
    			$v['age_xian'] = '<span style="color:red">'.$v['age_xian'].'</span>';
    		}
    	}
    	if (request()->isAjax()) {
    		$post = input('post.');
            // dump($post);die;
    		$res = db('popinfo')->insert($post);
            if ($res) {
                return 200;
            }else{
                return 202;
            }
            return $arr;
    	}
    	$this->assign('zhu',$zhu);
    	$this->assign('list',$list);

        $admin_cate_id = Session::get('admin_cate_id');
        $this->assign('admin_cate_id',$admin_cate_id);
        $operation = Config::get('operation');
        $this->assign('operation',$operation);

    	return $this->fetch();
    }
    public function info_data()
    {
    	$list = db('popinfo')->where(['isdel'=>1])->select();
    	$arr = [
    		'code'=>0,
    		'msg'=>'',
    		'data'=>$list
    	];
    	return json($arr); 
    }
}
